package com.mall.product.controller;

import java.util.Arrays;
import java.util.Map;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.mall.product.entity.BrandEntity;
import com.mall.product.entity.CategoryEntity;
import com.mall.product.service.BrandService;
import com.mall.product.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.mall.product.entity.CategoryBrandRelationEntity;
import com.mall.product.service.CategoryBrandRelationService;
import com.mall.common.utils.PageUtils;
import com.mall.common.utils.R;

import javax.validation.Valid;


/**
 * 品牌分类关联
 *
 * @author xzt
 * @email xzt@163.com
 * @date 2024-08-10 15:54:08
 */
@RestController
@RequestMapping("product/categorybrandrelation")
public class CategoryBrandRelationController {
    @Autowired
    private CategoryBrandRelationService categoryBrandRelationService;

    @Autowired
    private BrandService brandService;

    @Autowired
    private CategoryService categoryService;


    @GetMapping(value = "/brands/list")
    public R brandList(@RequestParam("catId") Long catId){
        QueryWrapper<CategoryBrandRelationEntity> wrapper =   new QueryWrapper<>();
        wrapper.eq("catelog_id",catId);
        return R.ok().put("data", categoryBrandRelationService.list(wrapper));
    }


    @GetMapping(value = "/catelog/list")
    public R categoryBrandRelationList(@RequestParam("brandId") String brandId){
        QueryWrapper<CategoryBrandRelationEntity> wrapper =   new QueryWrapper<>();
        wrapper.eq("brand_id",brandId);
        return R.ok().put("data", categoryBrandRelationService.list(wrapper));
    }
    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = categoryBrandRelationService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id){
		CategoryBrandRelationEntity categoryBrandRelation = categoryBrandRelationService.getById(id);

        return R.ok().put("categoryBrandRelation", categoryBrandRelation);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@Valid @RequestBody CategoryBrandRelationEntity categoryBrand){
        Long brandId = categoryBrand.getBrandId();
        Long categoryId = categoryBrand.getCatelogId();
       BrandEntity brand = brandService.getById(brandId);
       CategoryEntity category = categoryService.getById(categoryId);
        categoryBrand.setBrandName(brand.getName());
        categoryBrand.setCatelogName(category.getName());
		categoryBrandRelationService.save(categoryBrand);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody CategoryBrandRelationEntity categoryBrandRelation){
		categoryBrandRelationService.updateById(categoryBrandRelation);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] ids){
		categoryBrandRelationService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
